package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequiredPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiredPermission(name="公告页面",expression="notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo, HttpSession session){
        Employee employee =(Employee) session.getAttribute("USER_IN_SESSION");
        List<Long> readList = noticeService.markReadAll(employee.getId());
        Page<Notice> pageInfo = noticeService.queryPage(qo);
        model.addAttribute("markReadAll",readList);
        model.addAttribute("pageInfo", pageInfo);
        return "notice/list";
    }


    @RequiredPermission(name="公告删除",expression="notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.removeById(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice,HttpSession session) {
        if (notice.getId() == null) {
            noticeService.save(notice);
        } else {
            noticeService.updateById(notice);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expression="notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice",noticeService.getById(id));
        }
        return "notice/input";
    }

    @RequestMapping("/show")
    public String show(Long id, Model model,HttpSession session) {
        Employee employee =(Employee) session.getAttribute("USER_IN_SESSION");
        noticeService.insertReadStatus(employee.getId(), id);
        model.addAttribute("notice", noticeService.getById(id));
        //查询出这个用户已读的公告
        //noticeService.noticeHin(qo);
        return "notice/show";
    }
}
